package menu

import (
	"encoding/json"
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/pkg/response"
	"gitee.com/yangguoliang001/go-gin-vue3/util/code"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

// QueryMenuList 菜单列表
// @Summary 菜单列表
// @Description 菜单列表
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Success 200 {object} response.TResponse{data=[]vo.MenuItemVO}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu [get]
// @Security LoginToken
func (m *menuController) QueryMenuList(c *gin.Context) {
	menuList, err := m.menuService.QueryMenuList(c)
	if err == nil {
		response.Success(c, "获取菜单列表数据成功", gin.H{
			"menuList": menuList,
		})
	}
}

// QueryMenuItemDetail 菜单详情
// @Summary 菜单详情
// @Description 菜单详情
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id path string true "hashId"
// @Success 200 {object} response.TResponse{data=vo.MenuItemVO}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu/{id} [get]
// @Security LoginToken
func (m *menuController) QueryMenuItemDetail(c *gin.Context) {
	var id = c.Param("id")
	if id == "" {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": "id 必传"})
		return
	}
	menuItem, err := m.menuService.QueryMenuItemDetail(c, id)
	var menuItemMap = make(map[string]interface{})
	j, _ := json.Marshal(menuItem)
	_ = json.Unmarshal(j, &menuItemMap)

	if err == nil {
		response.Success(c, "获取菜单详情成功", menuItemMap)
	}
}

// EditMenu 创建/编辑菜单
// @Summary 创建/编辑菜单
// @Description 创建/编辑菜单
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param Request body QO.EditReq true "请求信息"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu [post]
// @Security LoginToken
func (m *menuController) EditMenu(c *gin.Context) {
	var menuReq QO.EditReq
	if err := c.ShouldBindJSON(&menuReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := m.menuService.EditMenu(c, menuReq)
	if err == nil {
		if menuReq.Id == -1 {
			response.Success(c, "创建菜单成功", gin.H{})
			return
		}
		response.Success(c, "编辑菜单成功", gin.H{})
	}
}

// UpdateMenuUsed 更新菜单为启用/禁用
// @Summary 更新菜单为启用/禁用
// @Description 更新菜单为启用/禁用
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id formData string true "hashId"
// @Param used formData int true "是否启用 1:是 -1:否"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu/used [patch]
// @Security LoginToken
func (m *menuController) UpdateMenuUsed(c *gin.Context) {
	var menuReq QO.MenuUsedReq
	if err := c.ShouldBind(&menuReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := m.menuService.UpdateMenuUsed(c, menuReq)
	if err == nil {
		response.Success(c, "菜单操作成功", gin.H{})
	}
}

// UpdateMenuSort 更新菜单排序
// @Summary 更新菜单排序
// @Description 更新菜单排序
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id formData string true "hashId"
// @Param sort formData int true "排序"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu/sort [patch]
// @Security LoginToken
func (m *menuController) UpdateMenuSort(c *gin.Context) {
	var menuReq QO.MenuSortReq
	if err := c.ShouldBind(&menuReq); err != nil {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": err})
		return
	}
	err := m.menuService.UpdateMenuSort(c, menuReq)
	if err == nil {
		response.Success(c, "菜单操作成功", gin.H{})
	}
}

// DeleteMenu 删除菜单
// @Summary 删除菜单
// @Description 删除菜单
// @Tags API.menu
// @Accept json
// @Produce json
// @Param id path int true "hashId"
// @Success 200 {object} response.TResponse{data=interface{}}
// @Failure 400 {object}  response.TResponse{data=interface{}}
// @Router /api/menu/{id} [delete]
// @Security LoginToken
func (m *menuController) DeleteMenu(c *gin.Context) {
	var id = c.Param("id")
	if id == "" {
		response.Fail(c, code.ParamBindError, code.Text(code.ParamBindError), gin.H{"realErr": "id 必传"})
		return
	}
	err := m.menuService.DeleteMenu(c, cast.ToInt32(id))

	if err == nil {
		response.Success(c, "菜单删除成功", gin.H{})
	}
}
